
/*******************************  FORM COMPONENT **********************************/
form = {};

form.serialize = function(component){
	var formData = $('#' + component).serializeArray();
	var formObject = {};

	for(var f in formData){
		formObject[formData[f].name] = formData[f].value;
	}

	return formObject;
}

form.submit = function(url, 
					   data,
					   method, 
					   successMessage, 
					   errorMessage){

	successMessage = successMessage || 'Registro Salvo com Sucesso';
	errorMessage = errorMessage || 'Ocorreu um erro ao salvar o registro';

	modal.carregando();
	$.ajax({
		url: url,
		data: data,
		type: method,
		success: function(result){
			if(result == 'ok'){
				if(!data.id)
					$('input').val('');
				modal.message(successMessage, 'success');
			} else{
				modal.message(result);
			}
		},
		error: function(result){
			modal.message(errorMessage)
		}
	}
	);

}

form.loadVar = function(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

/*******************************  MODAL COMPONENT **********************************/
var modal = {};
modal.carregando = function(){
	var img = $('<img/>');
	img.attr('src', base_url + '/assets/img/ajax-loader.gif');
	
	var h3 = $('<h3/>');
	h3.html('Carregando...');
	

	$('#modal .modal-body').html('<p id="message"></p>');
	$('#modal .modal-body').addClass('text-center');
	$('#modal .modal-body').append(img);
	$('#modal .modal-body').append(h3);
	$('#modal .modal-header').attr('style', 'display: none;');
	$('#modal .modal-footer').attr('style', 'display: none;');
	
	$('#modal').modal('show',{
		keyboard: false,
		backdrop: false
	});
};

modal.encerrar = function(){
	$('#modal').modal('hide');
}

modal.message = function(str, type){
	$('#modal .modal-footer').html(''); 
	$('#modal .modal-footer').append(bootstrap.button('OK', '', function(){ modal.encerrar(); } ));
	
	$('#myModalLabel').text("Atenção");
	$('#modal .modal-body').html('<h4 id="message"></h4>');
	$('#modal .modal-header').attr('style', '');
	$('#modal .modal-footer').attr('style', '');
	$("#modal #message").html(str);

	if(type == null)
		$("#modal #message").attr('class', 'text-error');
	else if(type == 'success')
		$("#modal #message").attr('class', 'text-success');

	$("#modal").modal('show');
}

modal.confirm = function(message, callbackSim, callbackNao){
	$('#myModalLabel').text("Atenção");
	var div = $('<div/>'); div.addClass("text-center");
	var yesButton = bootstrap.button('Sim', 'btn-primary');
	var noButton = bootstrap.button('Não', ''); 
	div.append(message); 

	if(callbackSim) yesButton.click(callbackSim);
	if(callbackNao) noButton.click(callbackNao);
	modal.html(div);

	$('#modal .modal-footer').html(''); 
	$('#modal .modal-footer').append(yesButton);
	$('#modal .modal-footer').append(noButton);
}

modal.html = function(html, ok){
	$("#modal .modal-body").removeClass("text-center");
	$('#modal .modal-header').attr('style', '');
	$('#modal .modal-footer').attr('style', '');
	$('#modal .modal-body').html('');
	$('#modal .modal-body').append(html);
	$("#modal").modal('show');
	if(ok != null) $('#okButton').click(ok);
}

/*************************** DATE **************************/
Date.prototype.format = function(format) //author: meizz
{
  var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
  }

  if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
      RegExp.$1.length==1 ? o[k] :
        ("00"+ o[k]).substr((""+ o[k]).length));
  return format;
}

function parse_date(input){
	  var parts = input.split('-');
	  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
	  return new Date(parts[0], parts[1]-1, parts[2]);
}

/****************************** BOOTSTRAP ************************/
bootstrap = {};
bootstrap.button = function(label, style, callback){
	var button = $("<button/>").attr("class", "btn " + style);
	button.text(label);
	if(callback)
		button.click(callback);
	return button;
}

bootstrap.label = function(label_text, style){
	var label = $('<label/>').attr('class', style);
	label.text(label_text);
	return label;
}


/****************************** BOOTSTRAP ************************/

util = {}

util.go_to = function(str){
	location.href = base_url_service + str;
}